package com.gln.bloger.action.admin;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorActionForm;

import com.gln.bloger.action.BaseAction;
import com.gln.bloger.constant.Constant;
import com.gln.bloger.exception.AlreadyExistException;
import com.gln.bloger.exception.BlogerException;
import com.gln.bloger.pojos.User;
import com.gln.bloger.pojos.Website;
import com.gln.bloger.util.StringUtil;

/**
 * Created on 20080115
 * @author renxj
 * modified user's web site
 */
public class WebsiteAction extends BaseAction{
	/**
	 * check authority
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			   HttpServletRequest request, HttpServletResponse response) throws Exception{
			User authUser = getAuthUser(request);
			if(authUser == null){
				ActionMessages errors = new ActionMessages();
				errors.add("errors", new ActionMessage("errors.no.authority"));
				saveErrors(request, errors);
				return mapping.findForward("login");
			}else{
				return super.execute(mapping, form, request, response);
			}
		}
	
	/**
	 * add  web site
	 * @param mapping
	 * @param form
	 * @param request
	 * @param responses
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward add(ActionMapping mapping, ActionForm form,
		   HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<String> themes = loadThemes(request);
		request.setAttribute("themes", themes);
		return mapping.findForward("websitePage");
	}
	/**
	 * edit 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			   HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<String> themes = loadThemes(request);
		Website currentWebsite =  getAuthWebsite(request);
		
		request.setAttribute("themes", themes);
		request.setAttribute("website", currentWebsite);
		return mapping.findForward("websitePage");
	}
	/**
	 * save
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			   HttpServletRequest request, HttpServletResponse response) throws Exception{
		ActionMessages errors = form.validate(mapping, request);
		
		DynaValidatorActionForm websiteForm = (DynaValidatorActionForm) form;
		String id = websiteForm.getString("id");
		String name = websiteForm.getString("name");
		String handle = websiteForm.getString("modifiedHandle");
		String description = websiteForm.getString("description");
		String summaryLimitLength = websiteForm.getString("summaryLimitLength");
		String ignoreWords = websiteForm.getString("ignoreWords");
		String subName = websiteForm.getString("subName");
		String theme = websiteForm.getString("theme");
		Website website;
		if(StringUtil.isBlank(id)){
			//insert
			website = new Website();
			website.setDateCreated(new Date());
			website.setCreator(getAuthUser(request));
		}else{
			website = getAuthWebsite(request);
		}
		website.setTheme(theme);
		website.setDescription(description);
		website.setEnabled(Boolean.TRUE);
		website.setHandle(handle);
		website.setIgnoreWords(ignoreWords);
		website.setLastModified(new Date());
		website.setName(name);
		website.setSubName(subName);
		website.setSummaryLimitLength(Integer.parseInt(summaryLimitLength));
		try{
			getWebsiteService().saveOrUpdateWebsite(website);
		}catch(AlreadyExistException e){
			errors.add("errors",new ActionMessage(
					"errors.handle.already.exist", website.getHandle()));
		}
		if(!errors.isEmpty()){
			saveErrors(request, errors);
			request.setAttribute("website", website);
			return mapping.getInputForward();
		}
		return mapping.findForward("chooseBlogger");
	}
	/**
	 * list
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		User user = getAuthUser(request);
		List<Website> websites = getWebsiteService().getWebsites(user);
		request.setAttribute("websites", websites);
		return mapping.findForward("listWebsitePage");
	}
	
	public ActionForward choose(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String handle = request.getParameter("handle");
		Website website = getWebsiteService().getWebsite(handle);
		request.getSession().setAttribute(Constant.AUTH_WEBSITE, website);
		/**
		 * cause web site has been changed, clear cache
		 */
		this.clearCache();
		return mapping.findForward("adminIndex");
	}
	
	@SuppressWarnings("unchecked")
	public List<String> loadThemes(HttpServletRequest request) throws BlogerException{
		List<String> themes = (List<String>)request.getSession().getServletContext().
                               getAttribute(Constant.AVAILABLE_THEMES);
		if(themes == null){
			themes = getWebsiteService().getThemes();
			request.getSession().getServletContext().
			            setAttribute(Constant.AVAILABLE_THEMES,themes);
		}
		return themes;
    }
}
